package com.sip.stream.utils.zfy.encoder;

import android.util.Log;

import com.gosuncn.zfyluancher.ZFYLauncherJNI;
import com.sip.stream.utils.GBUtils;
import com.sip.stream.utils.zfy.yuv.YUVToolFactory;

import io.reactivex.Single;
import io.reactivex.SingleObserver;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class G6H265ASyncEncoder extends AbstractVideoMediaCodecEncoder {
    private static final String TAG = "GSH265ASyncEncoder";

    private int tHeight;
    private int tWidth;

    public G6H265ASyncEncoder(int width, int height, int bitrate, int frameRate, int frameInterval) {
        super(width, height, bitrate, frameRate, frameInterval);
        this.tWidth = width;
        this.tHeight = height;
    }

    @Override
    public void onNV12Stream(byte[] bArr, final long j) {
        Single.just(bArr).subscribeOn(Schedulers.single()).subscribe(new SingleObserver<byte[]>() {
            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onSubscribe(Disposable disposable) {
            }

            @Override
            public void onSuccess(byte[] bArr2) {
                G6H265ASyncEncoder.this.encode_t(bArr2, j);
            }
        });
    }

    public synchronized void encode_t(byte[] bArr, long j) {
        ZFYLauncherJNI.NV12ToNV21(bArr, bArr, this.tWidth, this.tHeight);
        byte[] obtainOutputData = YUVToolFactory.obtainOutputData(bArr.length);
        int i = this.tWidth;
        int i2 = this.tHeight;
        ZFYLauncherJNI.scaleNV21_I420(bArr, i, i2, obtainOutputData, i, i2);
        YUVToolFactory.recycleData(bArr);
        super.onNV12Stream(obtainOutputData, j);
    }

    @Override
    protected boolean setUpMediacodec(int width, int height, int bitrate, int frameRate, int frameInterval) {
        Log.i(TAG, "setUpMediacodec " + width + "x" + height + " bitRate = " + ((bitrate / 1024) / 1024) + "mbps frameRate = " + frameRate);
        this.mediaCodec = GBUtils.createMediaCodec(width, height, bitrate, frameRate, frameInterval, "video/hevc", TAG);
        return this.mediaCodec != null;
    }

    @Override
    public boolean isStop() {
        return this.listeners.isEmpty();
    }
}
